<?php


namespace app\common\model;


use think\Db;

class Admin extends Base
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'ddt_admin';
    // 主键ID
    protected $pk = 'user_id';



    //新增、编辑(后台操作)
    function edit($params, $id=0){
        if($id>0){
            // 更新条件
            $where = ['user_id' => $id];
            $rs = $this->allowField(true)->save($params, $where);
        }else{
            // 更新数据
            $this->allowField(true)->save($params);
            $rs = $this->user_id;
        }
        if ($rs === false) {
            $this->setError(-1, '操作失败');
            return false;
        }
        return $rs;
    }


    // 查询列表
    function get_list($cond, $orderby, $item, $limit)
    {
        // 条件
        $where = null;
        if(!empty($cond['user_name'])){
            $where[] = ['a.user_name','like',"%".$cond['user_name']."%"];
        }

        if(!empty($cond['group_id'])){
            $where[] = ['a.group_id','=',$cond['group_id']];
        }
        if(!empty($cond['not_ins'])){
            $not_in = is_array($cond['not_ins'])?implode(',',$cond['not_ins']):$cond['group_id'];
            $where[] = ['a.group_id','not in',$not_in];
        }


        $rs = Db::table($this->table)
            ->alias('a')
            ->leftJoin('ddt_group b','a.group_id = b.group_id')
            ->where($where)
            ->field($item)
            ->order($orderby)
            ->paginate($limit);
        return $rs;
    }




}